package logic

import (
	"context"
	"fmt"
	"gorm.io/gorm"
	"yk/models/mysql"

	"yk/ykrpc/internal/svc"
	"yk/ykrpc/ykrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type YkUpdateLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewYkUpdateLogic(ctx context.Context, svcCtx *svc.ServiceContext) *YkUpdateLogic {
	return &YkUpdateLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *YkUpdateLogic) YkUpdate(in *ykrpc.YkUpdateRequest) (*ykrpc.YkUpdateResponse, error) {
	// todo: add your logic here and delete this line
	var yk mysql.Phone
	mysql.DB.Debug().Where("id =?", in.Id).Find(&yk)
	if yk.ID == 0 {
		return nil, fmt.Errorf("商品存在了,请不允许修改")
	}
	yks := mysql.Phone{
		Model:   gorm.Model{ID: uint(in.Id)},
		Name:    in.Name,
		Num:     int(in.Num),
		Address: in.Address,
	}
	mysql.DB.Debug().Updates(&yks)
	return &ykrpc.YkUpdateResponse{
		Id: int64(yks.ID),
	}, nil

}
